package icedot.library.common.ui;


import icedot.library.common.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 控件的排列为：Button
 * 			EditText
 * 			TextView
 * 			Button
 * 上下2个按钮来修改EditText和TextView的值
 * @author root
 *
 */

public class NumberButton extends LinearLayout
{
	public static final int s_datetimeType = 1;
	
	private Button 		_btnAdd;
	private Button 		_btnSub;
	private EditText	_editNum;
	private TextView	_textSubNum;
	
	private int 		_maxValue;
	private int 		_minValue;
	private int 		_nowValue;
	private int 		_stepValue;
	private ContentChangeNotify   _notify;
	
	private int 		_specialType;

	public NumberButton(Context context) 
	{
		super(context);
		initView(context);
	}
	
	public NumberButton(Context context, AttributeSet attrs) 
	{
		super(context, attrs);
		initView(context);
	}
	
	private void initView(Context context)
	{
		_maxValue = Integer.MAX_VALUE;
		_minValue = 0;
		_notify = null;
		_stepValue = 1;
		_specialType = 0;
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);  
		inflater.inflate(R.layout.btn_number, this);
		
		_btnAdd = (Button)findViewById(R.id.btnnumber_btn_add);
		_btnSub = (Button)findViewById(R.id.btnnumber_btn_sub);
		_editNum = (EditText)findViewById(R.id.btnnumber_edit_content);
		_textSubNum = (TextView)findViewById(R.id.btnnumber_text_subcontent);
		_textSubNum.setVisibility(View.GONE);
		
		_btnAdd.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				_nowValue += _stepValue;
				if(_nowValue >= _maxValue)
				{
					switch(_specialType)
					{
					case s_datetimeType:
						if(_nowValue > _maxValue)
						{
							_nowValue = _minValue;
						}
						break;
					default:
						_nowValue = _maxValue;
						break;
					}
					
				}
				updateContent();
			}
		});
		
		_btnSub.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v)
			{
				_nowValue -= _stepValue;
				if(_nowValue <= _minValue)
				{
					switch(_specialType)
					{
					case s_datetimeType:
						if(_nowValue < _minValue)
						{
							_nowValue = (_maxValue - _minValue + 1) + _nowValue;
						}
						break;
					default:
						_nowValue = _minValue;
						break;
					}
					
				}
				updateContent();
			}
		});
	}
	
	private void updateContent()
	{
		switch(_specialType)
		{
		case s_datetimeType:
			if(_nowValue < 10)
			{
				_editNum.setText("0" + String.valueOf(_nowValue));
			}
			else
			{
				_editNum.setText(String.valueOf(_nowValue));
			}
			break;
		default:
			_editNum.setText(String.valueOf(_nowValue));
			break;
		}
		
		if(_notify != null)
		{
			_notify.onContentChangeNotify(this,_nowValue);
		}
	}
	
	/**
	 * 设置子项是否显示
	 * @param visible
	 */
	public void setSubContentVisibility(int visible)
	{
		_textSubNum.setVisibility(visible);
	}
	
	public int getSubContentVisibility()
	{
		return _textSubNum.getVisibility();
	}
	
	public void setMaxNumber(int maxValue)
	{
		_maxValue = maxValue;
	}
	
	public void setMinNumber(int minValue)
	{
		_minValue = minValue;
	}
	
	public void setContentChangeNotify(ContentChangeNotify n)
	{
		_notify = n;
	}
	
	public void setContentText(int value)
	{
		if(value > _maxValue || value < _minValue)
			return;
		_nowValue = value;
		updateContent();
	}
	public void setSubContentText(int value)
	{
		_textSubNum.setText(String.valueOf(value));
	}
	
	/**
	 * 设置按钮的增量 默认为1 ,
	 * @param step
	 */
	public void setStepValue(int step)
	{
		_stepValue = step;
	}
	
	/**
	 * 表示该控件用于日期和时间
	 */
	public void setDateTimeType()
	{
		_specialType = s_datetimeType;
	}
	
	public interface ContentChangeNotify
	{
		public void onContentChangeNotify(View v,int value);
	}
}
